package br.senai.sc.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

import br.senai.sc.controller.crud.CorpoHidricoCRUD;
import br.senai.sc.model.CorpoHidrico;

/**
 * @author Fernando Marquardt &lt;fernando.marquardt@sc.senai.br&gt;
 * @since 01/02/2012
 * @version $Id: CorpoHidricoConverter.java 28 2012-03-07 20:48:20Z
 *          fernando.marquardt $
 */
@FacesConverter(forClass = CorpoHidrico.class)
public class CorpoHidricoConverter implements Converter {

	@Inject
	private CorpoHidricoCRUD corposHidricosCRUD;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		 return corposHidricosCRUD.findCorpoHidrico(Integer.valueOf(value));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (value instanceof CorpoHidrico) {
			return ((CorpoHidrico) value).getIdCorpoHidrico().toString();
		}

		return null;
	}
}